﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using EFCoreRepository.DbContexts;
using EFCoreRepository.Extensions;
using EFCoreRepository.Repositories;
using Zoomtel.Entity.BIZ;
using Zoomtel.Persist.BIZ.Models;
using Zoomtel.Persist.Query;
using System.Linq;
namespace Zoomtel.Persist.BIZ
{
    public class ProfitRepository : RepositoryAb<ProfitEntity>
    {
        public ProfitRepository(DefaultDbContext context) : base(context)
        {
        }

        public async Task<int> UpdateProfitAsync(ProfitEntity model)
        {
            int count = await DbContext.ExecuteSqlAsync("update T_BIZ_PROFIT SET profit=finishcount*@profit where ItemId=@itemid and Batch=@batch", new
            {
                batch = model.Batch,
                itemid = model.ItemId,
                profit = model.Profit
            });
            return count;
        }

        public async Task<QueryResultModel<ProfitModel>> QueryCalcResult(ProfitQueryMode model)
        {
            var paging = model.Paging();
            var items = DbContext.Set<ItemEntity>();
            var profits = DbContext.Set<ProfitEntity>();
            var agents = DbContext.Set<AgentInfoEntity>();
            
            var query = from profit in dbSet
                        join item in items on profit.ItemId equals item.ItemId
                        join agent in agents on profit.AgentId equals agent.Id
                        select new ProfitModel
                        {
                            Id = profit.Id,
                            AgentCode = agent.AgentCode,
                            AgentName = agent.AgentName,
                            ItemName = item.ItemName,
                            VisitCount = profit.VisitCount,
                            TestCount = profit.TestCount,
                            FinishCount = profit.FinishCount,
                            Profit = profit.Profit,
                            PerAmount = (profit.Profit / profit.FinishCount).ToString("#0.00"),
                            ProfitDate = profit.ProfitDate.ToShortDateString(),
                            Batch = profit.Batch,
                            ADTotalAmount = profit.ADTotalAmount,
                            ProfitType = profit.ProfitType,
                            ProfitTypeName= profit.ProfitType=="10"?"激励视频":"收徒抽成",
                            ItemId=profit.ItemId,
                            AgentId=profit.AgentId,
                            CreateTime=profit.CreateTime,
                            AuditFlag=profit.AuditFlag,
                            PlatForm=item.PlatForm
                        };
            if (model.Id != null && !"".Equals(model.Id))
            {
                query = query.Where(a => a.Id == model.Id);
            }
            if (model.Batch != null && !"".Equals(model.Batch))
            {
                query = query.Where(a => a.Batch == model.Batch);
            }
            if (model.AuditFlag != null && !"".Equals(model.AuditFlag))
            {
                query = query.Where(a => a.AuditFlag == model.AuditFlag);
            }
            if (model.AgentId != null && !"".Equals(model.AgentId))
            {
                query = query.Where(a => a.AgentId == model.AgentId);
            }
            if (model.ItemId != null && !"".Equals(model.ItemId))
            {
                query = query.Where(a => a.ItemId == model.ItemId);
            }
            if (model.AgentInfo != null && !"".Equals(model.AgentInfo))
            {
                query = query.Where(a => a.AgentCode.Contains(model.AgentInfo) || a.AgentName.Contains(model.AgentInfo));
            }
            if(model.ItemInfo != null && !"".Equals(model.ItemInfo))
            {
                query = query.Where(a => a.ItemName.Contains(model.ItemInfo));
            }
            if (model.PlatForm != null && !"".Equals(model.PlatForm))
            {
                query = query.Where(a => a.PlatForm == model.PlatForm);
            }
            if (!paging.OrderBy.Any())
            {
                query = query.OrderByDescending(m => m.CreateTime);
            }
            return await query.PaginationGetResult(paging);
        }

    }
}
